<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="100px">
      <el-form-item label="分类" prop="categoryIds">
        <el-cascader style="width: 100%" v-model="queryParams.categoryIds" :options="categoryOptions" :props="props" collapse-tags clearable
          ><template slot-scope="{ node, data }">
            <span>{{ data.label }}</span>
            <span v-if="!node.isLeaf"> ({{ data.children.length }}) </span>
          </template></el-cascader
        >
      </el-form-item>
      <el-form-item label="服务单编号" prop="letterNumber">
        <el-input v-model="queryParams.letterNumber" placeholder="请输入服务单编号" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="提交时间" prop="submitTime">
        <el-date-picker v-model="submitTime" :clearable="true" style="width: 240px" value-format="yyyy-MM-dd" type="daterange" range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期" @input="$forceUpdate()"></el-date-picker>
      </el-form-item>

      <el-form-item label="责任处室" prop="acceptDepts">
        <el-select v-model="queryParams.acceptDepts" placeholder="责任处室" clearable style="width: 100%">
          <el-option v-for="dict in dict.type.biz_letter_resp_dept" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>

      <el-form-item label="分类状态" prop="isCategory">
        <el-select v-model="queryParams.isCategory" placeholder="分类状态" clearable style="width: 100%">
          <el-option v-for="dict in dict.type.biz_whether_category" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd" v-hasPermi="['message:board:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate" v-hasPermi="['message:board:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete" v-hasPermi="['message:board:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport" v-hasPermi="['message:board:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="boardList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="一级分类" align="center" prop="firstCategoryName" />
      <el-table-column label="二级分类" align="center" prop="secondCategoryName" />
      <el-table-column label="服务单编号" align="center" prop="letterNumber" />
      <el-table-column label="标题" align="center" prop="letterTitle" :show-overflow-tooltip="true" />
      <el-table-column label="领域类型" align="center" prop="type" />
      <el-table-column label="交办类型" align="center" prop="letterType" />
      <el-table-column label="提交时间" align="center" prop="submitTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.submitTime, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <!-- <el-table-column
        label="留言内容"
        align="center"
        prop="letterContent"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="回复内容"
        align="center"
        prop="replyContent"
        :show-overflow-tooltip="true"
      /> -->
      <el-table-column label="回复时间" align="center" prop="replyDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.replyDate, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>

      <el-table-column label="责任处室" align="center" prop="acceptDepts">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.biz_letter_resp_dept" :value="scope.row.acceptDepts" />
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="createTime"></el-table-column>
      <!-- <el-table-column label="来信人ip" align="center" prop="letterIp" /> -->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="150">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row, 1)" v-hasPermi="['message:board:edit']">查看</el-button>
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row, 2)" v-hasPermi="['message:board:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)" v-hasPermi="['message:board:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="getList" />

    <!-- 添加或修改人民网留言板对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="50%" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="标题" prop="letterTitle">
              <el-input v-model="form.letterTitle" placeholder="请输入内容" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="留言ID" prop="messageId">
              <el-input v-model="form.messageId" placeholder="请输入留言ID" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="分类" prop="categoryIds">
              <el-cascader style="width: 100%" v-model="form.categoryIds" :options="categoryOptions" :props="{ expandTrigger: 'hover' }" clearable></el-cascader>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="责任处室" prop="acceptDepts">
              <el-select multiple v-model="form.acceptDepts" placeholder="责任处室" clearable style="width: 100%">
                <el-option v-for="dict in dict.type.biz_letter_resp_dept" :key="dict.value" :label="dict.label" :value="dict.value" />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="领域类型" prop="type">
              <el-input v-model="form.type" placeholder="请输入领域类型" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="交办类型" prop="type">
              <el-input v-model="form.letterType" placeholder="请输入交办类型" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="属地" prop="ownerArea">
              <el-select v-model="form.ownerArea" placeholder="请选择属地" clearable style="width: 100%">
                <el-option v-for="dict in dict.type.biz_area_owner" :key="dict.value" :label="dict.label" :value="dict.value" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="留言人属地" prop="leaveArea">
              <el-select v-model="form.leaveArea" placeholder="请选择留言人属地" clearable style="width: 100%">
                <el-option v-for="dict in dict.type.biz_area_owner" :key="dict.value" :label="dict.label" :value="dict.value" />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>

        <el-form-item label="留言内容">
          <editor v-model="form.letterContent" :min-height="150" />
        </el-form-item>
        <el-form-item label="回复内容">
          <editor v-model="form.replyContent" :min-height="150" />
        </el-form-item>

        <el-row>
          <el-col :span="12">
            <el-form-item label="提交时间" prop="submitTime">
              <el-date-picker style="width: 100%" clearable v-model="form.submitTime" type="datetime" value-format="yyyy-MM-dd HH:mm:ss" placeholder="请选择提交时间"> </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="回复时间" prop="replyDate">
              <el-date-picker style="width: 100%" clearable v-model="form.replyDate" type="datetime" value-format="yyyy-MM-dd HH:mm:ss" placeholder="请选择回复时间"> </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" v-if="view == 2" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listBoard, getBoard, delBoard, addBoard, updateBoard } from "@/api/hades/message/board";
import { treeselect } from "@/api/hades/category/category";

export default {
  name: "MessageBoard",
  dicts: ["biz_letter_resp_dept", "biz_whether_category", "biz_area_owner"],
  data() {
    return {
      props: { multiple: true },
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 人民网留言板表格数据
      boardList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        letterNumber: null,
        letterTitle: null,
        type: null,
        submitTime: null,
        letterType: null,
        acceptDept: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        categoryIds: [{ required: true, message: "请选择分类", trigger: "change" }],
        letterTitle: [{ required: true, message: "标题不能为空", trigger: "blur" }],
        // submitTime: [
        //   { required: true, message: "提交时间不能为空", trigger: "blur" },
        // ],
        letterType: [{ required: true, message: "交办类型不能为空", trigger: "change" }],
        letterContent: [{ required: true, message: "留言内容不能为空", trigger: "blur" }],
        replyContent: [{ required: true, message: "回复内容不能为空", trigger: "blur" }],
        // replyDate: [
        //   { required: true, message: "回复时间不能为空", trigger: "blur" },
        // ],
        acceptDept: [{ required: true, message: "办理部门不能为空", trigger: "blur" }],
        // messageId: [
        //   { required: true, message: "留言ID不能为空", trigger: "blur" },
        // ],
      },
      categoryOptions: [],
      view: 2,
      submitTime: null,
    };
  },
  created() {
    this.getList();
    this.getCategoryList();
  },
  methods: {
    /** 查询分类列表 */
    getCategoryList() {
      treeselect().then((response) => {
        this.categoryOptions = response.data;
      });
    },
    /** 查询人民网留言板列表 */
    getList() {
      this.loading = true;
      this.addDateRange(this.queryParams, this.submitTime, "SubmitTime");
      listBoard(this.queryParams).then((response) => {
        this.boardList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        pCategoryId: null,
        sCategoryId: null,
        letterNumber: null,
        letterTitle: null,
        type: null,
        submitTime: null,
        letterType: null,
        letterContent: null,
        replyContent: null,
        replyDate: null,
        acceptDept: null,
        letterIp: null,
        deptId: null,
        delFlag: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null,
        messageId: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.submitTime = null;
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.view = 2;
      this.reset();
      this.open = true;
      this.title = "添加人民网留言板";
    },
    /** 修改按钮操作 */
    handleUpdate(row, view) {
      this.view = view;
      this.reset();
      const id = row.id || this.ids;
      getBoard(id).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改人民网留言板";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updateBoard(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addBoard(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm('是否确认删除人民网留言板编号为"' + ids + '"的数据项？')
        .then(function () {
          return delBoard(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "message/board/export",
        {
          ...this.queryParams,
        },
        `board_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
